package lv.solcraft.replication.server.rest;

import org.restlet.Finder;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Resource;
import org.springframework.context.ApplicationContext;

public class SpringResourceFinder extends Finder {

    private final String resourceBeanName;
    private final ApplicationContext appContext;

    public SpringResourceFinder(String resourceBeanName, ApplicationContext appContext) {
        this.resourceBeanName = resourceBeanName;
        this.appContext = appContext;
    }

    @Override
    public Resource findTarget(Request request, Response response) {
        Resource resource = (Resource) appContext.getBean(resourceBeanName);
        resource.init(getContext(), request, response);
        return resource;
    }
}
